package com.leetcode.p300;

import com.utils.DataUtils;

import java.util.Arrays;
import java.util.HashSet;

public class Test349 {


    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> hashSet = new HashSet<>();

        for (int k : nums1) {
            hashSet.add(k);
        }

        int[] result = new int[hashSet.size()];

        int index = 0;

        for (int j : nums2) {
            if (hashSet.contains(j)) {
                result[index] = j;
                hashSet.remove(j);
                index++;
            }
        }

        return Arrays.copyOf(result, index);
    }

    public static void main(String[] args) {
        Test349 test = new Test349();

        int[] nums1 = new int[]{1, 2, 2, 1};
        int[] nums2 = new int[]{2, 2};

        int[] result = test.intersection(nums1, nums2);

        DataUtils.printArray(result);
    }
}
